package cn.xjt.ints;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author xujiangtao
 * @create 2020-08-04-14:14
 */
public class tcpService {
    public static void main(String[] args) throws IOException {
        String messageSend;             //定义一个字符串
        Scanner sc = new Scanner(System.in);             //输入函数
        ServerSocket server = new ServerSocket(8899);    //建立一个服务器网络，指定端口号
        Socket client = server.accept();          //接受一个客户端对象的连接，得到客户端对象
        if (client != null) {
            System.out.println("客户端连接成功");
        }
        InputStream inputStream = client.getInputStream();    //创建一个输入流
        OutputStream outputStream = client.getOutputStream();     //得到输出流
        for(int i=0;i<10;i++)
        {
            new Thread(()->{
                byte[] data = new byte[1024];      //定义一个字节数组
                int len = 0;        //读取数据
                try {
                    len = inputStream.read(data);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("你接受到一条消息："+new String(data,0,len));     //将数据输出
            }).start();

            new Thread(()->{

            }).start();
            messageSend = sc.nextLine();              //从控制台得到字符串
            if(messageSend.equals("end"))
            {
                sc.close();
                server.close();
                client.close();
            }

            outputStream.write(messageSend.getBytes());                 //将字符串发送

        }

    }
}
